﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace TE3
{
	public class Window : IDrawable
	{
		private static readonly Rectangle SkinTopLeftCorner = new Rectangle(0, 0, 3, 3);
		private static readonly Rectangle SkinLeft = new Rectangle(0, 3, 3, 27);
		private static readonly Rectangle SkinBottomLeftCorner = new Rectangle(0, 30, 3, 3);
		private static readonly Rectangle SkinTop = new Rectangle(3, 0, 27, 3);
		private static readonly Rectangle SkinCenter = new Rectangle(3, 3, 27, 27);
		private static readonly Rectangle SkinBottom = new Rectangle(3, 30, 27, 3);
		private static readonly Rectangle SkinTopRightCorner = new Rectangle(30, 0, 3, 3);
		private static readonly Rectangle SkinRight = new Rectangle(30, 3, 3, 27);
		private static readonly Rectangle SkinBottomRightCorner = new Rectangle(30, 30, 3, 3);

		private Rectangle _topLeftCorner;
		private Rectangle _left;
		private Rectangle _bottomLeftCorner;
		private Rectangle _top;
		private Rectangle _center;
		private Rectangle _bottom;
		private Rectangle _topRightCorner;
		private Rectangle _right;
		private Rectangle _bottomRightCorner;

		private readonly TE3Game _currentGame;
		public Rectangle Position;
		public int Scale;
		public string WindowTexture = "System/window.png";
		public bool Hidden = false;
		public Color Color;
		public Window(string name,Rectangle position, TE3Game game,Color color, int scale = 1)
		{
			_currentGame = game;
			Position = position;
			Color = color;
			Scale = scale;
			_currentGame.WindowManager.AddWindow(this, name);
			CalculatePosition();
		}
		public void Draw(SpriteBatch spriteBatch, GraphicsDevice device, TextureHandler handler)
		{
			if (Hidden)
				return;

			var windowTexture = handler.Textures[WindowTexture];

			spriteBatch.Draw(windowTexture, _topLeftCorner, SkinTopLeftCorner, Color);
			spriteBatch.Draw(windowTexture, _left, SkinLeft, Color);
			spriteBatch.Draw(windowTexture, _bottomLeftCorner, SkinBottomLeftCorner, Color);
			spriteBatch.Draw(windowTexture, _top, SkinTop, Color);
			spriteBatch.Draw(windowTexture, _center, SkinCenter, Color);
			spriteBatch.Draw(windowTexture, _bottom, SkinBottom, Color);
			spriteBatch.Draw(windowTexture, _topRightCorner, SkinTopRightCorner, Color);
			spriteBatch.Draw(windowTexture, _right, SkinRight, Color);
			spriteBatch.Draw(windowTexture, _bottomRightCorner, SkinBottomRightCorner, Color);

			spriteBatch.DrawString(_currentGame.FontManager.Fonts["Fonts/Console"], "Hello world!",
			                       new Vector2(Position.X + 40, Position.Y + 40), Color.Black);
		}
		public void CalculatePosition()
		{
			_topLeftCorner = new Rectangle(Position.X, Position.Y, 3*Scale, 3*Scale);
			
			_left = new Rectangle(Position.X, Position.Y + 3*Scale, 3*Scale, Position.Height - 3*(Scale*2));
			
			_bottomLeftCorner = new Rectangle(Position.X, Position.Y + Position.Height - 3*Scale, 3*Scale, 3*Scale);
			
			_top = new Rectangle(Position.X + 3*Scale, Position.Y, Position.Width - 6*Scale, 3*Scale);
			
			_center = new Rectangle(Position.X + 3*Scale, Position.Y + 3*Scale, Position.Width - 6*Scale,
			                        Position.Height - 6*Scale);
			
			_bottom = new Rectangle(Position.X + 3*Scale, Position.Y + Position.Height - 3*Scale,
			                        Position.Width - 6*Scale, 3*Scale);
			
			_topRightCorner = new Rectangle(Position.X + Position.Width - 3*Scale, Position.Y, 3*Scale, 3*Scale);
			
			_right = new Rectangle(Position.X + Position.Width - 3*Scale, Position.Y + 3*Scale, 3*Scale,
			                       Position.Height - 6*Scale);
			
			_bottomRightCorner = new Rectangle(Position.X + Position.Width - 3*Scale, Position.Y + Position.Height - 3*Scale,
			                                   3*Scale, 3*Scale);
		}
		public void Toggle()
		{
			if (Hidden)
				Show();
			else
				Hide();
		}
		public void Show()
		{
			Hidden = false;
		}
		public void Hide()
		{
			Hidden = true;
		}
	}
}
